package com.isunland.managesystem.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.CompanyForumAnswer;
import com.isunland.managesystem.entity.CompanyForumAnswer.actualObject;
import com.isunland.managesystem.entity.CompanyForumTopic;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 回复页面
 *
 * @author wu
 */
public class CompanyFroumTopicReplyFragment extends Fragment {

  private EditText mTopicReply;// 回复内容
  private BaseVolleyActivity mActivity;

  public static final String EXTRA_ID = "com.isunland.managesystem.ui.EXTRA_ITEM";
  public static final String EXTRA_VALUE = "com.isunland.managesystem.ui.EXTRA_VALUE";
  public static final String REQUEST_TYPE = "com.isunland.managesystem.ui.REQUEST_TYPE";
      // 判断是话题回复还是信息回复
  public static final String VALUE_TIPIC_REPLY = "com.isunland.managesystem.ui.VALUE_TIPIC_REPLY";
      // 话题回复type
  public static final String VALUE_MESSAGE_REPLY =
      "com.isunland.managesystem.ui.VALUE_MESSAGE_REPLY";// 信息回复type
  private String mTopicId;// 话题的id
  private CompanyForumAnswer.actualObject mAnswer;
  private CompanyForumTopic.actualObject mTopic;
  private String mPid; // 父节点的id
  private String mForumTopicPid;
  private String mReplyName;
  private String mType;
  private CheckBox mIsAnonymityCb;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.answer);
    mActivity = (BaseVolleyActivity) getActivity();
    mTopicId = getArguments().getString(EXTRA_ID);
    mType = getArguments().getString(REQUEST_TYPE);

    mAnswer = (actualObject) getArguments().getSerializable(EXTRA_VALUE);
    if (mAnswer != null) {
      mPid = mAnswer.getId();
      mForumTopicPid = mAnswer.getPid();
      LogUtil.i("——————————————" + mPid);
      mReplyName = mAnswer.getRegStaffName();
    }
    super.onCreate(savedInstanceState);
  }

  /**
   * 回复按钮
   */
  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_forum_topic_reply, container, false);
    mTopicReply = (EditText) view.findViewById(R.id.et_topic_reply);
    mIsAnonymityCb = (CheckBox) view.findViewById(R.id.cb_is_annoymity);
    return view;
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_forum_reply, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回按钮
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      // 响应回复按钮
      case R.id.menu_item_reply:
        contentExamineSave();
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  protected void contentExamineSave() {
    boolean isEmpty = MyUtils.isEmptyText(mTopicReply);
    if (isEmpty) {
      Toast.makeText(getActivity(), R.string.completeAnswerContent, Toast.LENGTH_SHORT).show();
      return;
    }
    volleyPostSave();
  }

  private void volleyPostSave() {
    MyUtils.showDialogLoading(getActivity());
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_REPLY);
    HashMap<String, String> params = new HashMap<String, String>();
    String answerDetail = mTopicReply.getText().toString();

    params.put("mainId", mTopicId);
    params.put("discussContents", answerDetail);
    if (VALUE_MESSAGE_REPLY.equals(mType)) {
      params.put("pid", mPid);
    }
    if (mIsAnonymityCb.isChecked()) {
      params.put("ifDisPlay", "T");
    } else {
      params.put("ifDisPlay", "F");
    }
    LogUtil.i("///////////" + params);

    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        try {
          LogUtil.i("response=" + arg0);
          MyUtils.dismissDialog();
          SuccessMessage success = new Gson().fromJson(arg0, SuccessMessage.class);
          if (null == success || null == success.getResult()) {
            Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
            return;
          }
          String successType = success.getResult();
          if (successType.equals(SuccessMessage.FAILURE)) {
            Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
            return;
          }
          if (successType.equals(SuccessMessage.SUCCESS)) {
            Toast.makeText(getActivity(), R.string.success_operation, Toast.LENGTH_SHORT).show();

            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
          }
        } catch (JsonSyntaxException e) {
          e.printStackTrace();
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {

      }
    });
  }

  public static Fragment newInstance(String id, actualObject topicAnswer, String type) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_ID, id);
    bundle.putSerializable(EXTRA_VALUE, topicAnswer);
    bundle.putString(REQUEST_TYPE, type);
    CompanyFroumTopicReplyFragment fragment = new CompanyFroumTopicReplyFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
